package com.lanou.beautifulwardrobe.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TabHost;
import android.widget.TextView;

import com.android.volley.Response;
import com.google.gson.Gson;
import com.lanou.beautifulwardrobe.Activity_zhuti.ActivityFragment;
import com.lanou.beautifulwardrobe.Brand.BrandFragment;
import com.lanou.beautifulwardrobe.Home.Adapter.HomeWaterItemAdapter;
import com.lanou.beautifulwardrobe.Home.HomeFragment;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.Space.SpaceFragment;
import com.lanou.beautifulwardrobe.contants.Contants;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.ShowProducts;
import com.lanou.beautifulwardrobe.entify.ShowproductResult;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by dllo on 15/11/2.
 */
public class ProductsShowActivity extends FragmentActivity implements View.OnClickListener {
    private TextView productType;
    //    private RecyclerView recyclerView;
    private ImageButton showProductBack;
    //    private HomeWaterItemAdapter adapter;
//    private GridLayoutManager manager;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private TabHost mHost;

    private RecyclerView newproRecyclerView;
    private HomeWaterItemAdapter newproAdapter;
    private GridLayoutManager newproManager;

    private RecyclerView hotsellRecyclerView;
    private HomeWaterItemAdapter hotsellAdapter;
    private GridLayoutManager hotsellManager;

    private RecyclerView priceRecyclerView;
    private HomeWaterItemAdapter priceAdapter;
    private GridLayoutManager priceManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_showproducts);

        initView();
        initData();
        // replaceFragment();
    }

    private void initView() {
        mHost = (TabHost) findViewById(android.R.id.tabhost);
        mHost.setup();
        TabHost.TabSpec newproSpec = mHost.newTabSpec("newproSpec");
        newproSpec.setIndicator(getLayoutInflater().inflate(R.layout.newpro_text, null));
        newproSpec.setContent(R.id.newproFrameLayout);

        TabHost.TabSpec hotsellSpec = mHost.newTabSpec("hotsellSpec");
        hotsellSpec.setIndicator(getLayoutInflater().inflate(R.layout.hotsell_text, null));
        hotsellSpec.setContent(R.id.hotsellFrameLayout);

        TabHost.TabSpec priceSpec = mHost.newTabSpec("priceSpec");
        priceSpec.setIndicator(getLayoutInflater().inflate(R.layout.price_text, null));
        priceSpec.setContent(R.id.priceFrameLayout);


        mHost.addTab(newproSpec);
        mHost.addTab(hotsellSpec);
        mHost.addTab(priceSpec);

        mHost.setCurrentTab(0);

        newproRecyclerView = (RecyclerView) findViewById(R.id.newproRecyclerView);
        hotsellRecyclerView = (RecyclerView) findViewById(R.id.hotsellRecyclerView);
        priceRecyclerView = (RecyclerView) findViewById(R.id.priceRecyclerView);
        newproAdapter = new HomeWaterItemAdapter();
        hotsellAdapter = new HomeWaterItemAdapter();
        priceAdapter = new HomeWaterItemAdapter();
        newproManager = new GridLayoutManager(this, 1);
        hotsellManager = new GridLayoutManager(this, 1);
        priceManager = new GridLayoutManager(this, 1);

        productType = (TextView) findViewById(R.id.productType);
        showProductBack = (ImageButton) findViewById(R.id.showProductBack);
        listener();
    }

//    private void replaceFragment() {
//
//        FragmentManager fm = getSupportFragmentManager();
//        FragmentTransaction ft = fm.beginTransaction();
//        ft.replace(R.id.homeFrameLayout, HomeFragment.getHomeFragment());
//        ft.replace(R.id.brandFrameLayout, new BrandFragment());
//        ft.replace(R.id.activityFrameLayout, ActivityFragment.getInstance());
//        ft.replace(R.id.spaceFrameLayout, SpaceFragment.getspaceFragment());
//        ft.commit();
//    }


    //        initView();
//        initData();
//    }
//
//    private void initView() {
//        productType = (TextView) findViewById(R.id.productType);
//        recyclerView = (RecyclerView) findViewById(R.id.showProductRecyclerView);
//        showProductBack = (ImageButton) findViewById(R.id.showProductBack);
//        adapter = new HomeWaterItemAdapter();
//        manager = new GridLayoutManager(this, 1);
//        listener();
//    }

    private void listener() {
        showProductBack.setOnClickListener(this);
    }

    private void initData() {
        Intent intent = getIntent();
        String titleName = intent.getStringExtra("titleName");
        try {
            String titleUrl = java.net.URLEncoder.encode(titleName, "utf-8");
            String newproUrl = Contants.WATERFALL_URL_PERFIX + titleUrl + Contants.WATERFALL_URL_NEWPROSUFFIX;
            String hotsellUrl = Contants.WATERFALL_URL_PERFIX + titleUrl + Contants.WATERFALL_URL_HOTSELLSUFFIX;
            String priceUrl = Contants.WATERFALL_URL_PERFIX + titleUrl + Contants.WATERFALL_URL_PRICESUFFIX;
            productType.setText(titleName);
            newproRecyclerView.setLayoutManager(newproManager);
            volleySingleton.getStringRequset(newproUrl, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Gson gson = new Gson();
                    ShowproductResult showproductResult = gson.fromJson(response, ShowproductResult.class);
                    if (showproductResult != null) {
                        List<ShowProducts> productsList = showproductResult.getData().getProducts();
                        newproAdapter.addData(productsList);
                        newproRecyclerView.setAdapter(newproAdapter);
                    }
                }
            });
            hotsellRecyclerView.setLayoutManager(hotsellManager);
            volleySingleton.getStringRequset(hotsellUrl, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Gson gson = new Gson();
                    ShowproductResult showproductResult = gson.fromJson(response, ShowproductResult.class);
                    if (showproductResult != null) {
                        List<ShowProducts> productsList = showproductResult.getData().getProducts();
                        hotsellAdapter.addData(productsList);
                        hotsellRecyclerView.setAdapter(hotsellAdapter);
                    }
                }
            });
            priceRecyclerView.setLayoutManager(priceManager);
            volleySingleton.getStringRequset(priceUrl, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Gson gson = new Gson();
                    ShowproductResult showproductResult = gson.fromJson(response, ShowproductResult.class);
                    if (showproductResult != null) {
                        List<ShowProducts> productsList = showproductResult.getData().getProducts();
                        priceAdapter.addData(productsList);
                        priceRecyclerView.setAdapter(priceAdapter);
                    }
                }
            });

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onClick(View v) {
        finish();
    }
}